<?php
namespace app\index\controller;
header("content-type:text/html;charset=utf-8");
use think\Controller;
class Product extends Common2
{
    public function index()
    {
        //商品分类
        $list = db('goods_fenlei')->select();
        //数组重组
        $tmp_list = $list;
        foreach ($tmp_list as $k1=>$v1){
            foreach ($tmp_list as $k2=>$v2){
                if($v2['pid']!=0 && $v2['pid']==$v1['id']){
                    $list[$k1]['son'][]=$v2;
                    unset($list[$k2]);
                }
            }
        }
        $this->assign('list',$list);
        // dump($list);die;
        return view();
    }

    public function product()
    {
        $fenlei_id = input('get.fenlei_id');
        $ret = db('goods_fenlei')->where("id={$fenlei_id}")->find();
        // dump($ret);die;
        $grade_one_fenlei_id = $ret['pid'];
        $grade_one_fenlei = db('goods_fenlei')->where("grade=1")->order('id asc')->select();

        $this->assign('grade_one_fenlei',$grade_one_fenlei);
        $this->assign('grade_one_fenlei_id',$grade_one_fenlei_id);
        $this->assign('fenlei_id',$fenlei_id);
        //获取二级分类
        $grade_two_fenlei = db('goods_fenlei')->where("grade=2 and pid={$grade_one_fenlei_id}")->order('id asc')->select();
        // dump($grade_two_fenlei);die;
        $this->assign('grade_two_fenlei',$grade_two_fenlei);
        return view();
    }

    public function product_search()
    {
        //模拟搜索查找数据
        if(isset($_GET['keywords'])){
            $keywords=$_GET['keywords'];
        }else{
            $keywords=input('post.keywords');
        }     
        // echo $keywords;die;
        if($keywords){
            $map['name']=['like','%'.$keywords.'%'];
            $row=db('goods')->where($map)->order('id desc')->select();
            $searchres=[];
            foreach( $row as $val )
            {
                $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
                if($img!=NULL){
                    $val['src']=array($img[0]);
                }else{
                    $val['src']=array($val['src']);
                }
                $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
                if($thumb_img!=NULL){
                    $val['thumb_src']=array($img[0]);
                }else{
                    $val['thumb_src']=array($val['thumb_src']);
                }
                $searchres[] = $val;  //存入新数组$row中
            }
        }else{
            $searchres = null;
            $keywords = '暂无数据';            
        }
        $this->assign('searchres',$searchres);
        $this->assign('keywords',$keywords);        
        // dump($searchres);die;
        //获取一级分类
        $grade_one_fenlei = db('goods_fenlei')->where("grade=1")->order('id asc')->select();
        $this->assign('grade_one_fenlei',$grade_one_fenlei);
        return view('product-search');
    }

    //商品详情页
    public function product_details()
    {
        //获取商品id
        $id = input('get.id');
        //拿商品id获取商品规格
        $list = db('goods_guigetype')->where("goods_id=$id")->order('id asc')->select();
        $tmp_list = $list;
        foreach ($tmp_list as $k1=>$v1){
            foreach ($tmp_list as $k2=>$v2){
                if($v2['pid']!=0 && $v2['pid']==$v1['id']){
                    $list[$k1]['son'][]=$v2;
                    unset($list[$k2]);
                }
            }
        }
        //获取第一个规格的第一个儿子的id
        if(isset($list[0]['son'])){
            $firstGuigeId = $list[0]['son'][0]['id'];
            //拿儿子的id获取固定属性
            $ret = db('goods_attributes')->where("guigeType_id={$firstGuigeId}")->find();
            $price = $ret['price'];
            $ziguige_list = $list[0]['son'];
            $this->assign('ziguige_list',$ziguige_list);
            
        }else{
            $ret = db('goods_attributes')->where("guigeType_id={$list[0]['id']}")->find();
            $price = $ret['price'];
        }
        // dump($list);die;
        session('sel_list',$list);
        $this->assign('price',$price);
        $this->assign('good_id',$id);
        $this->assign('list',$list);

        $goods = db('goods')->where("id=$id")->find();

        $img = json_decode($goods['src'],true);    //转换格式,json字符串->php数组
        if($img!=NULL){
            $goods['src']=$img;
        }else{
            $goods['src']=array($goods['src']);
        }
        $thumb_img = json_decode($goods['thumb_src'],true);    //转换格式,json字符串->php数组
        if($thumb_img!=NULL){
            $goods['thumb_src']=$thumb_img;
        }else{
            $goods['thumb_src']=array($goods['thumb_src']);
        }
        // dump($goods);die;
        $this->assign('goods',$goods);
        //获取评论内容
        $comment_data = db('comment')->alias('a')->join('user b','a.user_id=b.id')->field('a.*,b.name,b.headimg_url,b.wechat_headimgurl,b.weibo_headimgurl')->where("a.goods_id={$id}")->order("a.id desc")->select();
        //重组数组,获取评论回复人名字
        foreach ($comment_data as $k1 => $v1) {
            foreach ($comment_data as $k2 => $v2) {
                if($v2['pid']!=0 && $v1['id']==$v2['pid']){
                    $comment_data[$k2]['reply_user_id'] = $v1['user_id'];
                }
            }
        }
        foreach ($comment_data as $k => $v) {
            if(isset($v['reply_user_id'])){
                $ret = db('user')->where("id={$v['reply_user_id']}")->find();
                $comment_data[$k]['reply_name']=$ret['name'];
            }            
        }
        // dump($comment_data);die;
        $this->assign('comment_data',$comment_data);
        return view('product-details');
    }
    //我的优惠券页面(付款使用过程)
    public function coupon()
    {    
        $user_id = session('id');
        $coupon_list = db('coupon_user')->alias('a')->join('coupon b','a.coupon_id=b.id')->field('b.*')->where("a.user_id={$user_id}")->order('b.id asc')->select();
        if(isset($_GET['guigetype_id'])){
            $guigetype_id = input('get.guigetype_id');
            $this->assign('guigetype_id',$guigetype_id);
        }       
        $this->assign('coupon_list',$coupon_list);
        return view();
    }

    //Ajax获取第二个下拉框内容
    public function get_sel($id){
        $tmp_list = session('sel_list');
        $list = [];
        foreach ($tmp_list as $key => $value) {
            if($value['id']==$id){
                $list = $value;
            }
        }
        if(isset($list['son'])){
            $firstGuigeId = $list['son'][0]['id']; 
            $ret = db('goods_attributes')->where("guigeType_id={$firstGuigeId}")->find();
            $price = $ret['price'];
        }else{
            $ret = db('goods_attributes')->where("guigeType_id={$list['id']}")->find();
            $price = $ret['price'];
        }
        print_r(json_encode(['list'=>$list,'price'=>$price]));
    }
    public function get_price($guigeType_id){
        $list = db('goods_attributes')->where("guigeType_id={$guigeType_id}")->find();
        print_r(json_encode($list));
    }

    //评论商品
    public function comment(){
        $ret = input('post.');  
        if(session('id') == NULL || session('tell') == ""){
            $this->error("未登录!",'',0);
        }
        if(trim($ret['content']) == ""){
            $this->error("评论内容不能为空!",'',1);
        }
        $data = [
            'user_id'  => $ret['user_id'],
            'goods_id'  => $ret['goods_id'],
            'content'  => $ret['content'],
            'date'  => date('Y-m-d H:i:s',time()),
        ];
        $res = db('comment')->insert($data);
        if($res==FALSE){
            $this->error("发布失败!",'',2);
        }else{
            $this->success("发布成功!",'',3);
        }
    }
    //评论删除
    public function comment_delete($com_id){
        $res = db('comment')->where("id={$com_id}")->delete();
        if($res==FALSE){
            $this->error("删除失败!",'',1);
        }else{
            $this->success("删除成功!",'',2);
        }
    }
    //Ajax评论发表后刷新数据
    public function addAfter($goods_id){
        $res = db('comment')->alias('a')->join('user b','a.user_id=b.id')->field('a.*,b.name,b.headimg_url,b.wechat_headimgurl,b.weibo_headimgurl')->where("a.goods_id={$goods_id}")->order("a.id desc")->select();
        foreach ($res as $k1 => $v1) {
            foreach ($res as $k2 => $v2) {
                if($v2['pid']!=0 && $v1['id']==$v2['pid']){
                    $res[$k2]['reply_user_id'] = $v1['user_id'];
                }
            }
        }
        foreach ($res as $k => $v) {
            if(isset($v['reply_user_id'])){
                $ret = db('user')->where("id={$v['reply_user_id']}")->find();
                $res[$k]['reply_name']=$ret['name'];
            }            
        }
        print_r(json_encode($res));
    }
    //回复评论
    public function answer_comment(){
        $ret = input('post.');  
        if(session('id') == NULL || session('tell') == ""){
            $this->error("未登录!",'',0);
        }
        if(trim($ret['content']) == ""){
            $this->error("评论内容不能为空!",'',1);
        }
        $data = [
            'user_id'  => $ret['user_id'],
            'goods_id'  => $ret['goods_id'],
            'content'  => $ret['content'],
            'pid' => $ret['pid'],
            'date'  => date('Y-m-d H:i:s',time()),
        ];
        $res = db('comment')->insert($data);
        if($res==FALSE){
            $this->error("发布失败!",'',2);
        }else{
            $this->success("发布成功!",'',3);
        }
    }
    //Ajax检查库存是否为0
    public function checkKucun($guigetype_id=''){
        $guigetype_id = input('get.guigetype_id');
        $res = db('goods_guigetype')->where("id={$guigetype_id}")->find();
        $goods_id = $res['goods_id'];
        $id = input('get.id');
        $ret = db('goods_attributes')->where("guigeType_id={$guigetype_id}")->find();
        if($ret['kucun']==0){
            $this->error("该商品已售罄!",'index/index',1);
        }else{
            $this->redirect("http://localhost/index/product/product_details.html?id={$goods_id}");
        }
    }
}